﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Dynamic;
using M;
using Mysoft.MyCM.Scm;

namespace Mysoft.MyCM.Business.Scm
{
	/// <summary>
	/// 客户业务处理
	/// </summary>
	public class CustomBLL : BaseBLL
	{
		/// <summary>
		/// 根据TFS客户Code查询TFS客户唯一Id
		/// </summary>
		/// <param name="code">TFS客户Code</param>
		/// <returns></returns>
		public Guid GetGuid(string code)
		{
			MValidator.CheckForEmptyString("TFS客户代码", code);
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				var result = client.GetSync<ApiResult<Guid>>(string.Format("g/Custom/GetCustomGUID/{0}", code));
				CheckResult(result);
				return result.Result;
			}
		}

		/// <summary>
		/// 获取指定区域的TFS客户集合
		/// </summary>
		/// <param name="areaId">区域Id</param>
		/// <returns></returns>
		public List<Custom> GetCustomsOfArea(Guid areaId)
		{
			MValidator.CheckForEmptyGUID("区域Id", areaId);
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				var result = client.GetSync<ApiResult<List<Custom>>>(string.Format("g/Area/GetCustomByArea/{0}", areaId));
				CheckResult(result);
				return result.Result;
			}
		}

		/// <summary>
		/// 搜索客户
		/// </summary>
		/// <param name="keyword"></param>
		/// <returns></returns>
		public List<Custom> QueryCustoms(string keyword)
		{
			MValidator.CheckForEmptyString("搜索关键字", keyword);
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				var result = client.GetSync<ApiResult<List<Custom>>>(string.Format("g/Custom/GetCustomInfoLike/{0}", M.Web.Util.UrlEncode(keyword)));
				CheckResult(result);
				return result.Result;
			}
		}

		/// <summary>
		/// 获取TFS客户详情
		/// </summary>
		/// <param name="customId">TFS客户Id</param>
		/// <returns></returns>
		public Custom GetCustom(Guid customId)
		{
			MValidator.CheckForEmptyGUID("TFS客户Id", customId);
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				var result = client.GetSync<ApiResult<Custom>>(string.Format("g/Custom/GetCustomInfo/{0}", customId));
				CheckResult(result);
				return result.Result;
			}
		}

		/// <summary>
		/// 获取TFS客户详情
		/// </summary>
		/// <param name="code">TFS客户代码</param>
		/// <returns></returns>
		public Custom GetCustom(string code)
		{
			MValidator.CheckForEmptyString("TFS客户代码", code);
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				var result = client.GetSync<ApiResult<Custom>>(string.Format("g/Custom/GetCustomInfoByCode/{0}", code));
				CheckResult(result);
				return result.Result;
			}
		}

		/// <summary>
		/// 获取包含TFS信息的TFS客户详情
		/// </summary>
		/// <param name="customId">TFS客户Id</param>
		/// <returns></returns>
		public Custom2 GetCustom2(Guid customId)
		{
			MValidator.CheckForEmptyGUID("TFS客户Id", customId);
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				var result = client.GetSync<ApiResult<Custom2>>(string.Format("g/Custom/GetCustomInfo/{0}", customId));
				CheckResult(result);
				return this.getCustome2(result.Result);
			}
		}
		/// <summary>
		/// 获取包含TFS信息的TFS客户详情
		/// </summary>
		/// <param name="code">TFS客户代码</param>
		/// <returns></returns>
		public Custom2 GetCustom2(string code)
		{
			MValidator.CheckForEmptyString("TFS客户代码", code);
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				var result = client.GetSync<ApiResult<Custom2>>(string.Format("g/Custom/GetCustomInfoByCode/{0}", code));
				CheckResult(result);
				return this.getCustome2(result.Result);
			}
		}

		private Custom2 getCustome2(Custom2 custom)
		{
			// TFS地址
			TfsBLL tfsBLL = new TfsBLL();
			custom.TfsUrl = string.Format("{0}/{1}", tfsBLL.GetTfsServerInfo(custom.CustomGUID).TFSUrl, custom.Area.AreaShortName);
			// 项目集合根路径
			custom.ProjectCollectionBasePath = string.Format("$/{0}", custom.CustomName);
			return custom;
		}
	}
}
